/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aptech.eproject.controler;

import aptech.eproject.DBConnection;
import aptech.eproject.entity.BillDetails;
import java.sql.Connection;
import aptech.eproject.entity.Bills;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Ntienbo
 */
public class BillsControler {

    DBConnection db;
    Connection conn;

    public BillsControler() {
        db = new DBConnection();
    }

    public boolean insertBills(Bills b) {
        try {
            Date d = new Date(System.currentTimeMillis());
            conn = db.getConnect();
            PreparedStatement statement = conn.prepareStatement("INSERT INTO Bills VALUES (?,?,?,?,?,?)", ResultSet.CONCUR_UPDATABLE, ResultSet.TYPE_SCROLL_SENSITIVE);
            statement.setInt(1, b.getBillID());
            statement.setDate(2, d);
            statement.setInt(3, b.getDeptID());
            statement.setInt(4, b.getEmployeeID());
            statement.setInt(5, b.getCouponID());
            statement.setInt(6, b.getBillStatus());

            statement.executeUpdate();
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    public boolean deleteBills(Bills b) {
        try {
            PreparedStatement statement = conn.prepareStatement("DELETE BillDetails WHERE BillDetailID=?", ResultSet.CONCUR_UPDATABLE, ResultSet.TYPE_SCROLL_SENSITIVE);
            statement.setInt(1, b.getBillID());
            statement.executeUpdate();
            return true;
        } catch (SQLException ex) {
            return false;
        }

    }

    public boolean updateBills(Bills b) {
        try {
             Date d = new Date(System.currentTimeMillis());
            PreparedStatement statement = conn.prepareStatement("UPDATE Bills SET "
                    + "(BillDate=?,DeptID=?,EmployeeID=?,CouponID=?,BillStatus=?)  WHERE BillID=?");
            statement.setDate(1,d);
            statement.setInt(2, b.getDeptID());
            statement.setInt(3, b.getEmployeeID());
            statement.setInt(4, b.getCouponID());
            statement.setInt(5, b.getBillStatus());
            statement.setInt(6, b.getBillID());
            statement.executeUpdate();
            return true;
        } catch (SQLException ex) {
            return false;
        }

    }
}
